<?php

	class Ajax extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			
			$this->load->library('session');
			$this->load->database();
			$this->load->model('users_model', 'users');
		}
		
		public function do_login()
		{
			if($this->input->is_ajax_request())
			{
				$email = $this->input->post('email');
				$password = md5($this->input->post('password'));
				
				$result = $this->db->get_where('users', array('email' => $email, 'password' => $password));
				
				// OK
				if($result->num_rows() > 0)
				{
					// Set session
					$this->session->set_userdata('logged_in', TRUE);
					
					echo json_encode(array('result' => 1));
				}
				// No matches found!
				else
				{
					echo json_encode(array('result' => 0));
				}
			}
		}
		
		public function do_signup()
		{
			if($this->input->is_ajax_request())
			{
				$email = $this->input->post('email');
				$password = $this->input->post('password');
				
				// Connect?
				if($this->session->userdata('connect'))
				{
					$connect_data = $this->session->userdata('connect');
					
					// If user wants to connect by using twitter
					if(array_key_exists('twitter_id', $connect_data))
					{
						$twitter_id = $connect_data['twitter_id'];
						
						// Check if user exits...
						$result = $result = $this->db->get_where('users', array('email' => $email, 'password' => md5($password)));
						if($result->num_rows() > 0)
						{
							$user = $result->row();
							
							// User exists, update.
							$user->twitter_id = $twitter_id;
							$this->users->update($user);
							
							// Remove connect session
							$this->session->unset_userdata('connect');
							
							// Set session
							$this->session->set_userdata('logged_in', TRUE);
							
							echo json_encode(array('result' => 1));
							return;
							
						}
						// If doesn't, proceed with usual sign up.
					}	
				}		
				
				
				// Valid email address?
				if(preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $email))
				{				
					$result = $this->users->get_by_email($email);
					
					// We have a match!
					if($result)
					{
						echo json_encode(array('result' => 0));
					}
					// No matches found, feel free to use.
					else
					{
						// Connect?
						if($this->session->userdata('connect'))
						{
							// If user wants to connect by using twitter
							if(array_key_exists('twitter_id', $connect_data))
							{
								$twitter_id = $connect_data['twitter_id'];
								$result = $this->users->add($email, md5($password), NULL, NULL, $twitter_id);
							}
							// Facebook / Google
							else
							{
								$result = $this->users->add($email, md5($password));
							}
							// Was insert successful?
							if($result)
							{
								// Remove connect session
								$this->session->unset_userdata('connect');
								
								// Set session
								$this->session->set_userdata('logged_in', TRUE);
								
								echo json_encode(array('result' => 1));
							}
						}
						else
						{	
							// Was insert successful?
							if($this->users->add($email, md5($password)))
							{
								// Set session
								$this->session->set_userdata('logged_in', TRUE);
								
								echo json_encode(array('result' => 1));
							}
						}
					}
				}
				// Email not valid
				else
				{
					echo json_encode(array('result' => -1, 'extra' => $this->input->post('email')));
				}
			}
		}
		
	}

?>